package com.zjitc.controller;

import com.zjitc.Utils.JsonUtil;
import com.zjitc.common.StaticString;
import com.zjitc.model.User;
import com.zjitc.service.UserService;

import java.util.*;
import javax.xml.ws.RequestWrapper;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/9 16:10
 * @description:
 */
@Controller
public class UserController {

  @Autowired
  private UserService userService;

  //random：随机数

  static Random random=new Random();
  static String[] sex=new String[]{"n","m","-"};

  @RequestMapping("/insert.do")
  @ResponseBody
  public void insert(User user){
    List<User> userList = new ArrayList<>();
    final int count=5000;

    for (int i = 0; i < count; i++) {
      User u = new User();
      u.setName(user.getName());
      u.setSex(sex[random.nextInt(3)]);
      u.setAge(random.nextInt(100));
      userList.add(u);
    }

    //批量插入
    userService.batchInsert(userList);
  }

  @RequestMapping("list.do")
  @ResponseBody
  public String list(User user){
    Map<String, Boolean> resultMap = new HashMap<>(4);
    boolean isLessThan25 = user.getAge() <= 25 ? true : false;
    boolean isMoreThan17 = user.getAge() >= 17 ? true : false;
    boolean isWoman = "n".equals(user.getSex()) ? true : false;
    boolean isMan = "-".equals(user.getSex()) ? false : !isWoman;

    resultMap.put(StaticString.SET_AGE_LESS_THAN_25, isLessThan25);
    resultMap.put(StaticString.SET_AGE_MORE_THAN_17, isMoreThan17);
    resultMap.put(StaticString.SET_SEX_WOMEN, isWoman);
    resultMap.put(StaticString.SET_SEX_MEN, isMan);
    List userList = userService.list(resultMap);
    return JsonUtil.objToString(userList);
  }
}
